﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Xml.Serialization;

namespace Douban.Core.Settings
{
    [XmlRoot(ElementName = "settings")]
    public class SettingItems : ObservableObject
    {
        private float _windowOpacity;
        private HotKeySetting _getMessageHotkey;
        private double _refreshFrequency;
        private SendMessageHotKey _sendMessageHotKey;

        [XmlElement(ElementName = "opacity")]
        public float WindowOpacity
        {
            get { return _windowOpacity; }
            set 
            {
                if (value != _windowOpacity)
                {
                    _windowOpacity = value;
                    this.NotifyPropertyChanged("WindowOpacity");
                }
            }
        }

        [XmlElement(ElementName = "get-message-hotkey")]
        public HotKeySetting GetMessageHotKey
        {
            get { return _getMessageHotkey; }
            set
            {
                if (_getMessageHotkey != value)
                {
                    _getMessageHotkey = value;
                    NotifyPropertyChanged("GetMessageHotKey");
                }
            }
        }

        /// <summary>
        /// Measured by seconds
        /// </summary>
        [XmlElement(ElementName = "refresh-frequency")]
        public double RefreshFrequency
        {
            get { return _refreshFrequency == 0 ? 1 : _refreshFrequency; }
            set
            {
                if (_refreshFrequency != value)
                {
                    _refreshFrequency = value;
                    NotifyPropertyChanged("RefreshFrequency");
                }
            }
        }

        [XmlElement(ElementName = "send-message-hotkey")]
        public SendMessageHotKey SendMessageHotKey
        {
            get { return _sendMessageHotKey; }
            set
            {
                if (_sendMessageHotKey != value)
                {
                    _sendMessageHotKey = value;
                    NotifyPropertyChanged("SendMessageHotKey");
                }
            }
        }

        public SettingItems Clone()
        {
            SettingItems clone = new SettingItems();
            var settingItemsProperties = TypeDescriptor.GetProperties(this);
            foreach (PropertyDescriptor propDescriptor in settingItemsProperties)
            {
                propDescriptor.SetValue(
                    clone,
                    propDescriptor.GetValue(this));
            }
            return clone;
        }
    }
}
